package org.example.springboot.common;

import java.util.HashMap;
import java.util.Map;

//统一返回的包装类Result
//包装类：作用是统一后端返回的数据类型
public class Result {
    private String code;
    private String msg;
    private Object data;
    private Map<String, Object> extra;
    public Map<String, Object> getExtra() {
        return extra;
    }

    public void setExtra(Map<String, Object> extra) {
        this.extra = extra;
    }


    //写一个静态方法返回Result类型数据 返回成功是的信息
    public static Result success(){
        Result result = new Result();
        result.setCode("200");
        result.setMsg("success");
        return result;
    }
    public static Result success(Object data){
        Result result = success();
        result.setData(data);
        result.setExtra(new HashMap<>());
        return result;
    }
    public Result put(String key, Object value) {
        if (this.extra != null) {
            this.extra.put(key, value);
        }
        return this;
    }
    //写一个静态方法返回Result类型数据 返回失败时的信息
    public static Result error(){
        Result result = new Result();
        result.setCode("500");
        result.setMsg("系统错误");
        return result;
    }
    public static Result error(String code , String msg){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public Result() {
    }

    public Result(String code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
